<template>
  <a-spin :spinning="todo.isLoading">
    <a-list
      :data-source="todo.result"
      item-layout="horizontal"
      :split="true"
      class="container"
    >
      <template #renderItem="{ item }">
        <a-list-item>
          <div class="row">
            <div class="head">
              <a-checkbox
                :checked="item.completed"
                @change="toggleComplete(item)"
              ></a-checkbox>
            </div>
            <div class="content">
              <div class="title">
                <span>{{ item.summary }}</span>
                <a-button shape="circle" @click="toggleContent(item)"
                  ><template #icon
                    ><DownOutlined v-if="!item.showContent" class="icon-size" />
                    <UpOutlined v-else class="icon-size" /> </template
                ></a-button>
              </div>
              <div class="detail" v-if="item.showContent">
                <pre>
              {{ item.detail }}
              </pre
                >
              </div>
              <div class="control">
                <a-button type="primary">编辑</a-button>
                <a-button type="default">存档</a-button>
              </div>
            </div>
          </div>
        </a-list-item>
      </template>
    </a-list>
  </a-spin>
</template>

<script setup>
import { defineProps, onMounted, computed, ref, shallowRef } from "vue";
import { DownOutlined, UpOutlined } from "@ant-design/icons-vue";
import { useGetTodo } from "../../api";

const todo = useGetTodo();
onMounted(async () => {
  await loadData();
});

const loadData = async () => {
  await todo.exec();
};

const toggleContent = (item) => {
  item.showContent = !item.showContent;
};
const toggleComplete = (item) => {
  item.completed = !item.completed;
};

const toggleExpand = (item) => {
  item.expanded = !item.expanded;
};
</script>

<style scoped>
.container {
  display: block;
  width: 100%;
  height: auto;
}

.row {
  display: flex;
  align-items: flex-start;
  width: 100%;
  height: auto;
  gap: 5px;
}

.head {
  display: flex;
}

.content {
  display: flex;
  flex-grow: 1;
  flex-direction: column;
  gap: 2px;
}

.title {
  display: flex;
  width: 100%;
  align-items: center;
  font-weight: bolder;
  gap: 8px;
  button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 28px;
    background-color: #01ff70;
  }
  .icon-size {
    font-size: smaller;
  }
}
.control {
  display: flex;
  gap: 8px;
}

.detail {
  font-size: smaller;
}
.summary {
  display: inline-block;
  max-width: calc(100% - 200px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  vertical-align: middle;
}

.buttons-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.edit-button {
  background-color: #ffeb3b;
  border-color: #ffeb3b;
}

.ant-list-item {
  margin-bottom: 16px;
}
</style>
